package com.code.common.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: Yan
 * @date: 2023/03/29
 * 统一封装实体
 *
 * success：返回操作是否成功
 * code：返回状态码
 * message：返回描述消息
 * data：返回数据
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Builder
public class ResultVO<T> implements Serializable {

    private Boolean success;
    private String code;
    private String message;
    private T data;

    public static <T> ResultVO<T> OK(){
        return new ResultVO<>(Boolean.TRUE, ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), null);
    }

    public static <T> ResultVO<T> OK(T data){
        if (data instanceof ErrorCode) {
            return OK((ErrorCode) data);
        }
        return new ResultVO<>(Boolean.TRUE, ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), data);
    }

    private static <T> ResultVO<T> OK(ErrorCode data) {
        return new ResultVO<>(Boolean.TRUE, data.getCode(), data.getMsg(), null);
    }

    public static <T> ResultVO<T> ERROR(){
        return new ResultVO<>(Boolean.FALSE, ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg(), null);
    }

    public static <T> ResultVO<T> ERROR(T data){
        if (data instanceof ErrorCode) {
            return ERROR((ErrorCode) data);
        }
        return new ResultVO<>(Boolean.FALSE, ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg(), data);
    }

    private static <T> ResultVO<T> ERROR(ErrorCode data) {
        return new ResultVO<>(Boolean.FALSE, data.getCode(), data.getMsg(), null);
    }

}
